// 导入db模块
const db = require('../common/db/index')
// 获取当前时间的函数
const { getCurrentTime, getRandomIndex , user_imgs } = require('../common/javascript/index')

// 定义用户类
class User {
    // 注册
    register(username,password,email){
        let index = getRandomIndex(user_imgs.length)
        let avatar_url = user_imgs[index]//随机用户头像
        // YYYY-MM-DD hh:mm:ss
        let create_at = getCurrentTime()// 注册时间
        return new Promise((resolve,reject)=>{
                db.query(`select email from pre_users where email = '${email}'`)
                .then(
                    rows=> {
                        if(rows.length == 0) {
                            db.query(`insert into pre_users(username,password,email,avatar_url,create_at) values('${username}','${password}','${email}','${avatar_url}','${create_at}')`)
                            .then(
                                () => {
                                    resolve({
                                        code: 200,
                                        msg: "恭喜您，注册成功",
                                        email
                                    })
                                }
                            )
                            .catch(
                                err=> {
                                    reject({
                                        code: 500,
                                        msg: "后台发生异常",
                                        err 
                                    })
                                }
                            )
                        }
                        else {
                            resolve({
                                code: 1,
                                msg: "该用户已存在",
                                email
                            })
                        }
                    }
                )
                .catch(
                    err=> {
                        reject({
                            code: 500,
                            msg: "后台发生异常",
                            err 
                        })
                    }
                )
        })
    }
    
    // 登录
    login (password,email){
        return new Promise((resolve,reject)=>{
            db.query(`select email, user_id, username,avatar_url,create_at from pre_users where password='${password}' and email='${email}'`)
            .then(
                rows=> {
                    if(rows.length == 0 ) {
                        resolve({
                            code: -1,
                            msg: "该用户不存在"
                        })
                    }
                    else {
                        resolve({
                            code: 200,
                            msg: "恭喜您，登录成功",
                            result: [... rows]
                        })
                    }
                }
            )
            .catch(
                err=> {
                    reject({
                        code: 500,
                        msg: "后台发生异常",
                        err 
                    })
                }
            )
            
        })
    }

    // 列表
    list (page=1, pageSize=15){
        return new Promise((resolve,reject)=>{
            // 统计表的总记录数量
            db.query(`select count(*) as total from pre_users`)
            .then(
                content=> {
                    let page_num = ((parseInt(page) - 1) * parseInt(pageSize))
                    let page_size= (parseInt(pageSize))
                    // 分页列表
                    db.query(`select  email, user_id, username,avatar_url,create_at  from pre_users limit ${page_num} , ${page_size}`)
                    .then(
                        rows=> {
                            resolve({
                                code: 200,
                                msg: "获取列表成功",
                                total:content[0] &&  content[0].total,
                                result: [... rows]
                            })
                        }
                    )
                    .catch(
                        err=> {
                            reject({
                                code: 500,
                                msg: "后台发生异常",
                                err 
                            })
                        }
                    )
                }
            )
            .catch(
                err=> {
                    reject({
                        code: 500,
                        msg: "后台发生异常",
                        err 
                    })
                }
            )
        })
    }

    // 搜索
    search(value){
        // console.log("xxx+",value)
        return new Promise((resolve,reject)=>{
            db.query(`select email, user_id, username,avatar_url from pre_users where user_id='${value}' or email='${value}'`)
            .then(
                rows=> {
                    resolve({
                        code: 200,
                        msg: "查询完毕",
                        result: [...rows]
                    })
                }
            )
            .catch(
                err=> {
                    reject({
                        code: 500,
                        msg: "后台发生异常",
                        err 
                    })
                }
            )
        })
    }

    //删除
    remove(user_id){
        return new Promise((resolve,reject)=>{
            db.query(`select email, user_id from pre_users where user_id='${user_id}'`)
            .then(
                rows=> {
                   if(rows.length == 0) {
                        resolve({
                            code: -1,
                            msg: "该用户不存在"
                        })
                   }
                   else {
                       //删除
                       db.query(`delete from pre_users where user_id = '${user_id}'`)
                       .then(
                          data=> {
                              resolve({
                                code: 200,
                                msg: "删除账号成功",
                                data 
                              })
                          }
                       )  
                       .catch(
                        err=> {
                            reject({
                                code: 500,
                                msg: "后台发生异常",
                                err 
                            })
                        }
                    )

                   }
                }
            )
            .catch(
                err=> {
                    reject({
                        code: 500,
                        msg: "后台发生异常",
                        err 
                    })
                }
            )
        })
    }

    // 根据用户id获取昵称、头像
    info(user_id){
        return new Promise((resolve,reject)=>{
            db.query(`select  username,avatar_url,user_id from pre_users where user_id='${user_id}'`)
            .then(
                rows=> {
                   resolve({
                    code: 200,
                    msg: "用户信息",
                    result: [... rows]
                   })
                }
            )
            .catch(
                err=> {
                    reject({
                        code: 500,
                        msg: "后台发生异常",
                        err 
                    })
                }
            )
        })
    }
}

// 把用户类的实例挂在模块系统对象上
module.exports = User;